import request from '../../utils/request'

Page({
  data: {
    posts: [],
    page: 1,
    pageSize: 10,
    hasMore: true,
    backendUrl: 'http://127.0.0.1:88/api/user/'
  },

  onLoad() {
    this.loadPosts();
  },

  onPullDownRefresh() {
    this.setData({
      page: 1,
      posts: [],
      hasMore: true
    }, () => {
      this.loadPosts();
    });
  },

  onReachBottom() {
    if (this.data.hasMore) {
      this.loadPosts();
    }
  },

  loadPosts() {
    const userInfo = wx.getStorageSync('userInfo');
    if (!userInfo) {
      wx.showToast({
        title: '请先登录',
        icon: 'none'
      });
      return;
    }

    wx.showLoading({
      title: '加载中...'
    });

    wx.request({
      url: this.data.backendUrl + 'post.php',
      method: 'GET',
      data: {
        action: 'myPosts',
        user_id: userInfo.id,
        page: this.data.page,
        pageSize: this.data.pageSize
      },
      success: (res) => {
        if (res.data.status === 'success') {
          const newPosts = res.data.posts;
          newPosts.forEach(post => {
            if (post.avatar) {
              post.avatar = this.data.backendUrl + post.avatar;
            }
            if (post.image) {
              post.image = post.image;
            }
          });
          this.setData({
            posts: [...this.data.posts, ...newPosts],
            page: this.data.page + 1,
            hasMore: newPosts.length === this.data.pageSize
          });
        }
      },
      complete: () => {
        wx.hideLoading();
        wx.stopPullDownRefresh();
      }
    });
  },

  goToDetail(e) {
    const { id } = e.currentTarget.dataset;
    wx.navigateTo({
      url: `/pages/post-detail/post-detail?id=${id}`
    });
  },

  goToPostEdit() {
    wx.navigateTo({
      url: '/pages/post-edit/post-edit'
    });
  },

  deletePost(e) {
    const { id } = e.currentTarget.dataset;
    
    wx.showModal({
      title: '确认删除',
      content: '确定要删除这条帖子吗？',
      success: (res) => {
        if (res.confirm) {
          const userInfo = wx.getStorageSync('userInfo');
          if (!userInfo) {
            wx.showToast({
              title: '请先登录',
              icon: 'none'
            });
            return;
          }
          
          wx.showLoading({ title: '删除中...' });
          
          wx.request({
            url: this.data.backendUrl + 'post.php',
            method: 'POST',
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            data: {
              action: 'deletePost',
              post_id: id,
              user_id: userInfo.id
            },
            success: (res) => {
              if (res.data.status === 'success') {
                wx.showToast({
                  title: '删除成功',
                  icon: 'success'
                });
                // 重新加载帖子列表
                this.setData({
                  page: 1,
                  posts: [],
                  hasMore: true
                }, () => {
                  this.loadPosts();
                });
              } else {
                wx.showToast({
                  title: res.data.message || '删除失败',
                  icon: 'none'
                });
              }
            },
            fail: () => {
              wx.showToast({
                title: '删除失败',
                icon: 'none'
              });
            },
            complete: () => {
              wx.hideLoading();
            }
          });
        }
      }
    });
  }
}); 